package org.loofer.uiframework.navigator.backstack;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import java.util.ArrayList;
import java.util.List;

public class FragmentBackStackManager {
    private FragmentManager mAttachFragmentManager;
    private int mContainerViewRes;
    private int mEnterAnimRes;
    private List<AbstractFragment> mFragmentList = new ArrayList();
    private int mPopExitAnimRes;
    private AbstractFragment mRootFragment;

    public FragmentBackStackManager(FragmentManager attachFragmentManager) {
        this.mAttachFragmentManager = attachFragmentManager;
    }

    public void setLaunchFragmentAttr(int containerViewRes, int enterAnimRes, int popExitAnimRes) {
        this.mContainerViewRes = containerViewRes;
        this.mEnterAnimRes = enterAnimRes;
        this.mPopExitAnimRes = popExitAnimRes;
    }

    public void launchFragment(AbstractFragment fragment, int containId, int enterAnimRes, int popExitAnimRes) {
        handleClearTopMode();
        if (!handleSingleTopMode(fragment)) {
            handleTopFragment();
            fragment.onNewResume();
            this.mFragmentList.add(fragment);
            if (containId <= 0) {
                containId = this.mContainerViewRes;
            }
            this.mAttachFragmentManager.beginTransaction().setCustomAnimations(enterAnimRes, 0, 0, popExitAnimRes).add(containId, (Fragment) fragment).addToBackStack(null).commitAllowingStateLoss();
        }
    }

    public void launchFragment(AbstractFragment fragment) {
        launchFragment(fragment, this.mContainerViewRes, this.mEnterAnimRes, this.mPopExitAnimRes);
    }

    public void hidePreviousFragment(AbstractFragment fragment) {
        if (fragment != null && fragment.isHiddenPreviousFragment() && getTopFragment() == fragment) {
            AbstractFragment previousFragment = getPreviousFragment();
            if (previousFragment != null && previousFragment.getView() != null) {
                previousFragment.getView().setVisibility(8);
            }
        }
    }

    public boolean isBackStackEmpty() {
        return this.mFragmentList.isEmpty();
    }

    public void clear() {
        this.mRootFragment = null;
        this.mFragmentList.clear();
    }

    public AbstractFragment getTopFragment() {
        if (this.mFragmentList.isEmpty()) {
            return this.mRootFragment;
        }
        return (AbstractFragment) this.mFragmentList.get(this.mFragmentList.size() - 1);
    }

    public void launchRootFragment(AbstractFragment fragment) {
        if (this.mContainerViewRes == 0) {
            throw new IllegalArgumentException("you must call setLaunchFragmentAttr(int containerViewRes, int enterAnimRes, int popExitAnimRes) first");
        }
        fragment.onNewResume();
        this.mRootFragment = fragment;
        this.mAttachFragmentManager.beginTransaction().add(this.mContainerViewRes, (Fragment) fragment).commitAllowingStateLoss();
    }

    public AbstractFragment getRootFragment() {
        return this.mRootFragment;
    }

    public List<AbstractFragment> getFragmentList() {
        return this.mFragmentList;
    }

    public void setSubRootFragment(AbstractFragment fragment) {
        this.mRootFragment = fragment;
    }

    public void destroyFragment(AbstractFragment fragment) {
        if (isRootFragment(fragment)) {
            throw new IllegalArgumentException("Primary fragment must not be finish");
        }
        try {
            if (this.mFragmentList.size() > 0 && this.mFragmentList.get(this.mFragmentList.size() - 1) == fragment) {
                this.mAttachFragmentManager.popBackStack();
                AbstractFragment popFragment = pop();
                if (popFragment != null) {
                    popFragment.onNewPause();
                }
                AbstractFragment curFragment = getTopFragment();
                if (curFragment != null) {
                    curFragment.onNewResume();
                    if (curFragment.mRequestCode >= 0 && popFragment != null) {
                        curFragment.onActivityResult(curFragment.mRequestCode, popFragment.mResultCode, popFragment.mResultData);
                        curFragment.mRequestCode = -1;
                        popFragment.mResultCode = 0;
                        popFragment.mResultData = null;
                    }
                    View view = curFragment.getView();
                    if (view != null) {
                        view.setVisibility(0);
                    }
                }
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    public final void showPreviousFragment() {
        AbstractFragment previousFragment = getPreviousFragment();
        if (previousFragment != null && previousFragment.getView() != null) {
            previousFragment.getView().setVisibility(0);
        }
    }

    public final void hidePreviousFragment() {
        AbstractFragment previousFragment = getPreviousFragment();
        if (previousFragment != null && previousFragment.getView() != null) {
            previousFragment.getView().setVisibility(8);
        }
    }

    private AbstractFragment pop() {
        AbstractFragment fragment = (AbstractFragment) this.mFragmentList.get(this.mFragmentList.size() - 1);
        this.mFragmentList.remove(fragment);
        return fragment;
    }

    public boolean isRootFragment(AbstractFragment fragment) {
        return this.mRootFragment == fragment;
    }

    private AbstractFragment getPreviousFragment() {
        int size = this.mFragmentList.size();
        if (size > 1) {
            return (AbstractFragment) this.mFragmentList.get(size - 2);
        }
        if (size == 1) {
            return this.mRootFragment;
        }
        return null;
    }

    protected FragmentManager getAttachFragmentManager() {
        return this.mAttachFragmentManager;
    }

    private void handleTopFragment() {
        AbstractFragment topFragment = getTopFragment();
        if (topFragment != null) {
            topFragment.onNewPause();
        }
    }

    private void handleClearTopMode() {
        AbstractFragment topFragment = getTopFragment();
        if (topFragment != null && topFragment.isClearTop()) {
            destroyFragment(topFragment);
        }
    }

    private boolean handleSingleTopMode(AbstractFragment fragment) {
        AbstractFragment top = getTopFragment();
        if (top == null || !fragment.isSingleTop() || fragment.getClass() != top.getClass()) {
            return false;
        }
        if (top.getActivity() != null) {
            top.onNewBundle(fragment.getArguments());
        }
        fragment.finish();
        return true;
    }
}
